﻿local gsub = string.gsub

function Macaroon.SetAnchorChild(child, anchor)

	if (not child or child == "") then return end

	local name = child.bar.config.name

	if (anchor.config.clickAnchor or anchor.config.mouseAnchor) then

		child:SetAttribute("isBarChild", nil)
		child:SetAttribute("isAnchorChild", true)
		child:Hide()

		child.oldparent = child:GetParent()
		child:SetParent(anchor:GetName())

		anchor.config.anchoredBar = name
	else
		child:SetParent(child.oldparent:GetName() or "UIParent")
		child:SetAttribute("isAnchorChild", nil)

		anchor.config.anchoredBar = false

		if (child.bar and child.bar.updateVisibility) then
			child.bar.updateVisibility(child.bar, child:GetAttribute("state-current"))
		end
	end

	if (anchor.config.clickAnchor) then
		child:SetAttribute("_childupdate-onclick", [[

					if (message == "show") then
						self:Show()
					else
						self:Hide()
					end

					]])

	else
		child:SetAttribute("_childupdate-onclick", nil)
	end

	if (anchor.config.mouseAnchor) then
		child:SetAttribute("_childupdate-onmouse", [[

					if (message == "enter") then
						self:Show()
					elseif (not self:IsUnderMouse(true)) then
						self:Hide()
					else
						control:SetTimer(self:GetAttribute("mousedelay") or 0, "leave")
					end

					]])

		child:SetAttribute("_ontimer", [[

					if (not self:IsUnderMouse(true)) then
						self:Hide()
					else
						control:SetTimer(self:GetAttribute("mousedelay") or 0, "leave")
					end

					]])
		child:SetAttribute("mousedelay", tonumber(anchor.config.anchorDelay) or 0)
	else
		child:SetAttribute("_childupdate-onmouse", nil)
		child:SetAttribute("mousedelay", nil)
		child:SetAttribute("_ontimer", nil)
	end
end

function Macaroon.SetAnchor(anchor)

	anchor:SetParent("UIParent")

	if (anchor.config.clickAnchor == "") then
		anchor.config.clickAnchor = false
	end

	if (anchor.config.mouseAnchor == "") then
		anchor.config.mouseAnchor = false
	end

	if (anchor.config.clickAnchor) then

		anchor:SetAttribute("click-show", "hide")
		anchor:WrapScript(anchor, "OnClick", [[

				if (self:GetAttribute("click-show") == "hide") then
					self:SetAttribute("click-show", "show")
				else
					self:SetAttribute("click-show", "hide")
				end

				control:ChildUpdate("onclick", self:GetAttribute("click-show"))

				]])

	else

		anchor:UnwrapScript(anchor, "OnClick")
		anchor:SetAttribute("click-show", nil)
	end

	if (anchor.config.mouseAnchor) then
		anchor:WrapScript(anchor, "OnEnter", [[ control:ChildUpdate("onmouse", "enter") ]])
		anchor:WrapScript(anchor, "OnLeave", [[ control:SetTimer(self:GetAttribute("mousedelay") or 0, "leave") ]])
		anchor:SetAttribute("mousedelay", tonumber(anchor.config.anchorDelay) or 0)
		anchor:SetAttribute("_ontimer", [[ control:ChildUpdate("onmouse", message) ]])
	else
		anchor:UnwrapScript(anchor, "OnEnter")
		anchor:UnwrapScript(anchor, "OnLeave")
		anchor:SetAttribute("mousedelay", nil)
		anchor:SetAttribute("_ontimer", nil)
	end

	if (anchor.config.anchoredBar) then

		local bar = Macaroon.BarIndexByName[anchor.config.anchoredBar]

		if (bar) then
			Macaroon.SetAnchorChild(bar.handler, anchor)
		end
	end

	if (anchor.bar and anchor.bar.updateBar) then
		anchor.bar.updateBar(anchor.bar, nil, true, true)
	end

end